package cn.com.yusys.yusp.custManager.web.rest;


import cn.com.yusys.yusp.custManager.domain.OcrmFCiFqDbcol;
import cn.com.yusys.yusp.custManager.service.FlexibleQueryService;
import cn.com.yusys.yusp.commons.excel.ExcelImportExportUtil;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import groovy.json.JsonException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author: zoie
 * @Description: 灵活查询
 * @Date: 2021/5/1
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/flexibleQuery")
@Api(tags = "flexibleQuery")
public class FlexibleQueryResource extends CommonResource<OcrmFCiFqDbcol, String> {

    @Autowired
    private FlexibleQueryService flexibleQueryService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 客户灵活查询查询左侧树
     * @param
     * @return HashMap
     */
    @GetMapping("/getIndexTree")
    @ApiOperation(value = "获取客户灵活查询左侧树", notes = "获取客户灵活查询查询左侧树")
    public ResultDto<List<Map<String, Object>>> getIndexTree() {
        return new ResultDto<>(this.flexibleQueryService.getIndexTree());
    }

    /**
     * 客户灵活查询查询字段属性值类型 文本框..
     *
     * @param
     * @return HashMap
     */
    @GetMapping("/showColType")
    @ApiOperation(value = "查询字段属性值类型", notes = "查询查询字段属性值类型")
    public ResultDto<List<Map<String, Object>>> showColType(QueryModel model) {
        List<Map<String, Object>> list = this.flexibleQueryService.showColType(model);
        return new ResultDto<>(list);
    }

    @Override
    protected CommonService getCommonService() {
        return null;
    }

    /**
     * 客户灵活查询结果
     *
     * @param
     * @return HashMap
     */
    @GetMapping("/queryResult")
//    @OperLog(operModul = "客户灵活查询结果",operType = "查询数据日志")
    @ApiOperation(value = "客户灵活查询结果", notes = "客户灵活查询结果")
    public ResultDto<List<Map<String, Object>>> queryResult(QueryModel model) {
        ResultDto<List<Map<String, Object>>> resultDto = new ResultDto<List<Map<String, Object>>>();
        List<Map<String, Object>> list = this.flexibleQueryService.queryResult(model, true);
        if (list == null) {
            resultDto.setMessage("执行查询出错，请检查方案是否合理！");
            resultDto.setCode(-1);
            return resultDto;
        } else {
            return new ResultDto<>(list);
        }
    }

    @GetMapping("/queryDownloadInfo")
    public ResultDto<Map<String,Object>> queryInfo(@RequestParam("taskId") String taskId) throws JsonException {
        ResultDto<Map<String, Object>> resultDto = new ResultDto<>();
        HashMap<String, Object> map = new HashMap<>();
        resultDto.setData(map);
        JSONObject jo = ExcelImportExportUtil.getRedisTaskInfo(taskId, stringRedisTemplate);
        Iterator<String> keys = jo.keys();
        while (keys.hasNext()){
            String key = keys.next();
            map.put(key,jo.get(key));
        }
        return resultDto;
    }
}
